package com.backstage.employeeModule.controller;


import com.backstage.employeeModule.pojo.Employee;
import com.backstage.employeeModule.service.EmployeeService;
import com.backstage.userModule.pojo.User;
import com.backstage.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * `Mobile_end_Employee` 前端控制器
 * </p>
 *
 * @author liu-xiang
 * @since 2022-12-13
 */
@RestController
@RequestMapping("/emp")
@CrossOrigin
public class EmployeeController {
  @Autowired
  private EmployeeService employeeService;

  @GetMapping("list")
  public R list(){
    List<Employee> empList = employeeService.list();
    if(empList == null)
      return R.error();
    return R.ok().data("empList",empList);
  }

  @PostMapping("login")
  public R login(@RequestBody Employee employee){
    Employee emp =  employeeService.login(employee.getUsername(),employee.getPassword());
    return Objects.isNull(emp)?R.error():R.ok().data("emp",emp);
  }

  @PostMapping("save")
  public R save(@RequestBody Employee emp){
    return employeeService.save(emp)?R.ok():R.error();
  }
}

